package com.club.football.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.club.football.dto.ClubSaveDto;
import com.club.football.dto.PageDto;
import com.club.football.entity.Club;
import com.club.football.req.ClubPageReq;
import com.club.football.service.ClubService;
import com.club.football.utils.FileUtils;
import com.club.football.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.club.football.common.CommonConstants.DEL_OK_MSG;
import static com.club.football.common.CommonConstants.GET_INFO_OK;

/**
 * 俱乐部Controller
 *
 * @author 1
 * @date 2023/12/28
 */
@RestController
@RequestMapping("/club")
public class ClubController {
    @Resource
    private ClubService clubService;

    /**
     * 俱乐部列表
     *
     * @return {@link R}
     */
    @GetMapping("/page")
    public R<PageDto<Club>> page(PageDto<Club> pageDto, ClubPageReq clubPageReq) {
        return clubService.clubPage(pageDto, clubPageReq);
    }

    /**
     * 保存
     *
     * @param saveDto 将数据保存到
     * @return {@link R}<{@link String}>
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody @Valid ClubSaveDto saveDto) {
        return clubService.clubSave(saveDto);
    }

    /**
     * 信息
     *
     * @param id ID
     * @return {@link R}<{@link Club}>
     */
    @GetMapping("/info/{id}")
    public R<Club> info(@PathVariable Long id) {
        Club club = clubService.getById(id);
        String clubLogo = club.getClubLogo();
        if (StringUtils.isNotBlank(clubLogo)) {
            club.setClubLogo(FileUtils.getUrl(club.getClubLogo()));
        }
        return R.ok(club, GET_INFO_OK);
    }

    /**
     * 删除
     *
     * @param id ID
     * @return {@link R}<{@link String}>
     */
    @DeleteMapping("/remove/{id}")
    public R<String> remove(@PathVariable Long id) {
        clubService.removeById(id);
        return R.ok(DEL_OK_MSG);
    }
}
